package co.crossoft.daoImpl;

import co.crossoft.domain.BomBean;
import co.crossoft.domain.BomComponentBean;
import co.crossoft.mapper.BomConfigMapper;
import co.crossoft.util.JsonConvertUtil;
import co.crossoft.util.JsonReturnMsg;
import com.fasterxml.jackson.core.JsonProcessingException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Snow on 8/15/2017.
 */
@Repository("bomDaoImpl")
public class BomDaoImpl {

    @Autowired
    BomConfigMapper bomConfigMapper;

    public String getBomList(String plant,String id,String version){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        Map map = new HashMap<>();
        String retString = "";
        if(!"".equals(version)){
            map.put("version",version);
        }
        if(plant.equals("")){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("plant can not be empty!");
        }
        if(jsonReturnMsg.getRetCode().equals("1")){
            map.put("plant",plant);
            map.put("id",id);
            List<BomBean> bomList = bomConfigMapper.bomListByBom(map);
            if(bomList.size()>0){
                Map<String,List> retMap = new HashMap();
                BomBean bomBean = bomList.get(0);
                retMap.put("BomList",bomList);
                if(!plant.equals("")&&!id.equals("")&&!version.equals("")){
                    List bomComList = excuteMehtodComponentList("",bomBean.getQuid());
                    retMap.put("BomComList",bomComList);
                }
                jsonReturnMsg.setRetObj(retMap);
            }else{
                jsonReturnMsg.setRetCode("0");
                jsonReturnMsg.setRetMsg("no data return!");
            }
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String addOrUpdateBom(BomBean bomBean){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if( bomBean.getPlant().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("plant can not be empty!");
        }else if( bomBean.getId().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("bom's name can not be empty!");
        }else if( bomBean.getVersion().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("version can not be empty!");
        }else if( jsonReturnMsg.getRetCode().equals("1") ){
            // 根据bom名字和版本和工厂查询数据库中是否存在该笔数据，存在更新，反之新增
            List list = bomConfigMapper.bomListByBomAndVersion(bomBean);
            if ( list.size()>0 ){
                bomConfigMapper.updateBom(bomBean);
            }else{
                bomConfigMapper.addBom(bomBean);
            }
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String deleteBomData(String plant,String id,String version){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        String quid="BOM:"+plant+","+id+","+version;
        if( plant.equals("") || id.equals("") || version.equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("required column can not be empty!");
        }else{
            bomConfigMapper.delteBom(quid);
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String deleteBomAndComp(String quid){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if( quid.equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("required column can not be empty!");
        }else{
            bomConfigMapper.delteBom(quid);
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String addOrUpdateComponent(BomComponentBean bean){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if( bean.getSequence() == null ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("sequence can not be empty!");
        }else if( bean.getBomFk().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("bom can not be empty!");
        }else if( bean.getMaterialFk().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("material can not be empty!");
        }else if( bean.getMaterialFk().equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("material can not be empty!");
        }else if( bean.getQty() == null || bean.getQty() < 0 ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("qty can not be less than zero or null");
        }
        if(jsonReturnMsg.getRetCode().equals("1")){
            Map parMap = new HashMap();
            parMap.put("id",bean.getId());
            List list = bomConfigMapper.bomComponentList(parMap);
            if(list.size()>0){
                bomConfigMapper.updateBomComponent(bean);
            }else{
                bomConfigMapper.addBomComponent(bean);
            }
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String getBomComponentList(String id, String bomFk){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        List list = excuteMehtodComponentList(id,bomFk);
        jsonReturnMsg.setRetList(list);
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    // 查询组清单被调用方法
    public List excuteMehtodComponentList(String id, String bomFk){
        Map map = new HashMap();
        if(!id.equals("")){
            map.put("id",id);
        }else if(!bomFk.equals("")){
            map.put("bomFk",bomFk);
        }
        List list = bomConfigMapper.bomComponentList(map);
        return list;
    }

    public String createVirMaterial(String id, String name, String version, String parentBomId,String plant){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if(id.equals("") || name.equals("") || parentBomId.equals("") || plant.equals("")){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("required column can not be empty!");
        }else{
            String materialId = "MATERIAL:"+plant+","+id+","+version;
            String bomId = "BOM:"+plant+","+id+","+version;
            String bomComponentId = "BOMCOM:"+materialId+","+bomId+","+10;
            Map map = new HashMap();
            map.put("id",id);
            map.put("name",name);
            map.put("version",version);
            map.put("parentBomId",parentBomId);
            map.put("plant",plant);
            map.put("bomComponentId",bomComponentId);
            map.put("materialId",materialId);
            map.put("bomId",bomId);
            bomConfigMapper.createNewViMaterial(map);
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    public String dialogComSelect(String quid,String id){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        Map paraMap = new HashMap();
        if(!quid.equals("")){
            paraMap.put("quid",quid);
        }
        if(!id.equals("")){
            paraMap.put("id",id);
        }
        List list = bomConfigMapper.dialogComSelect(paraMap);
        jsonReturnMsg.setRetList(list);
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    // 删除物料清单组件
    public String deleteBomComponent(String bomComponentJson){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if(!bomComponentJson.equals("")){
            String[] str = bomComponentJson.split("&");
            if(str.length>0){
                for(String id : str){
                    bomConfigMapper.deleteBomComponent(id);
                }
            }else{
                jsonReturnMsg.setRetCode("0");
                jsonReturnMsg.setRetMsg("web param format has error!");
            }
        }else{
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("deleted component id can not be empty!");
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

    // bom拆分移动组件到新BOM
    public String reDistributeBomComponent(String souBomCompIdString,String destBomId ){
        JsonReturnMsg jsonReturnMsg = new JsonReturnMsg("1","success");
        String retString = "";
        if( souBomCompIdString.equals("") || destBomId.equals("") ){
            jsonReturnMsg.setRetCode("0");
            jsonReturnMsg.setRetMsg("required column can not be empty");
        }else{
            String[] compString = souBomCompIdString.split("&");
            Map<String,List> retMap = new HashMap();
            List list = null;
            if( compString.length>0 ){
                for(String bomComponentId : compString){
                    Map parMap = new HashMap();
                    String newCompId =  "BOMCOM:"+bomComponentId+":"+destBomId;
                    parMap.put("newId",newCompId);
                    parMap.put("newBomFk",destBomId);
                    parMap.put("sourceId",bomComponentId);
                    bomConfigMapper.reDistributeBomComponent(parMap);
                    list.add(newCompId);
                }
            }
            retMap.put("newCompIdList",list);
            jsonReturnMsg.setRetObj(retMap);
        }
        try {
            retString = JsonConvertUtil.beanToJson(jsonReturnMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return retString;
    }

}
